package com.huanglj.taobaounion.ui.adapter;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.model.domain.SelectedByCategory;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedPageRightAdapter extends RecyclerView.Adapter<SelectedPageRightAdapter.InnerHolder> {
    private static final String TAG = "SelectedPageRightAdapte";

    private List<SelectedByCategory.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mData = new ArrayList<>();
    private OnButtonClickListener mListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_right, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<SelectedByCategory.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> result) {
        mData.clear();
        mData.addAll(result);
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.pick_item_cover)
        ImageView pickCover;
        @BindView(R.id.pick_off_prise)
        TextView offPrise;
        @BindView(R.id.pick_origin_prise)
        TextView originPrise;
        @BindView(R.id.pick_item_buy_btn)
        TextView itemBuy;
        @BindView(R.id.pick_item_title)
        TextView title;
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        @SuppressLint("SetTextI18n")
        public void setData(SelectedByCategory.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {
            LogUtil.d(TAG,mapDataBean.getTitle());
            title.setText(mapDataBean.getTitle());
            String pict_url = mapDataBean.getPict_url();
            String coverPath = UrlUtils.getCoverPath(pict_url, 400);
            Glide.with(itemView.getContext()).load(coverPath).into(pickCover);

            if (TextUtils.isEmpty((CharSequence) mapDataBean.getCoupon_click_url())) {
                originPrise.setText("晚啦，没有优惠券了");
                itemBuy.setVisibility(View.GONE);
            }else {
                itemBuy.setVisibility(View.VISIBLE);
                originPrise.setText("原价: " +mapDataBean.getZk_final_price());
            }
            if (TextUtils.isEmpty(mapDataBean.getCoupon_info())) {
                offPrise.setVisibility(View.GONE);
            }else {
                offPrise.setVisibility(View.VISIBLE);
                offPrise.setText(mapDataBean.getCoupon_info());
            }
            itemBuy.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onClickListener(mapDataBean.getTitle(),mapDataBean.getClick_url(),mapDataBean.getPict_url());
                }
            });
        }
    }

    public interface OnButtonClickListener {
        void onClickListener(String title,String url,String pickCoverUrl);
    }

    public void setonButtonClickListener(OnButtonClickListener listener){
        this.mListener = listener;
    }
}
